package com.seecen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.mapper.RecipesMapper;
import com.seecen.pojo.Favorites;
import com.seecen.service.FavoritesService;
import com.seecen.mapper.FavoritesMapper;
import com.seecen.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* @author W
* @description 针对表【t_favorites(用户收藏表)】的数据库操作Service实现
* @createDate 2025-07-21 12:57:25
*/
@Service
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites>
    implements FavoritesService{

    @Autowired
    private FavoritesMapper favoritesMapper;

    @Override
    public Result getFavoriteById(Integer favoriteId) {

        Favorites favorites = favoritesMapper.selectById(favoriteId);
        HashMap data = new HashMap();
        data.put("favorites",favorites);

        return Result.ok(data);
    }

    @Override
    public Result getFavoriteAll() {
        List<Favorites> favorites = favoritesMapper.selectList(null);
        return Result.ok(favorites);
    }

    @Override
    public Result insertFavoriteAll(Favorites favorites) {
        try {
            // 1. 参数校验
            if (favorites.getUserId() == null) {
                return Result.ok("用户ID不能为空");
            }
            if (favorites.getRecipeId() == null) {
                return Result.ok("菜谱ID不能为空");
            }

            favorites.setCreateTime(new Date());
            int insert = favoritesMapper.insert(favorites);
            if (insert > 0) {
                return Result.ok("收藏成功");
            } else {
                return Result.ok("收藏失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("服务器错误: " + e.getMessage());
        }
        }

    @Override
    public Result deleteFavorite(Integer recipeId) {

        int deleted = favoritesMapper.deleteByRecipeId(recipeId);
        return Result.ok("删除成功");
    }

    @Override
    public Result selectFavoriteByUserId(Integer userId) {
        if (userId==null){
            return Result.ok("用户不存在");
        }
        List<Integer> integers = favoritesMapper.selectByUserId(userId);
        return Result.ok(integers);
    }
}




